package com.kafka.logs.service;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.context.annotation.PropertySource;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Log4j2
@Component
@PropertySource("classpath:rabbitmq.properties")
public class RabbitMQDirectReceiver {

    @RabbitListener(queues = "DirectQueue")
    public void process(Map message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {

        final String[] mapString = {""};

        message.forEach((key,value) -> {
            mapString[0] += key.toString() + ":" + value.toString() + ",";
        });

        try {
            channel.basicAck(tag, false);
            log.info("deliveryTag为：("+ tag + ") 被确认");
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }

        log.info("DirectReceiver消费者收到消息  : " + mapString[0]);
    }

}
